package icu.qx.pro.service.impl;

import java.util.List;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.pro.domain.ProSchedule;
import icu.qx.pro.domain.ProWorkTask;
import icu.qx.pro.mapper.ProWorkTaskMapper;
import icu.qx.pro.mq.ProStatusConsumer;
import icu.qx.pro.service.IProScheduleService;
import icu.qx.pro.service.IProWorkTaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import icu.qx.pro.mapper.ProWorkOrderMapper;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.service.IProWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产工单Service业务层处理
 *
 * @author yong
 * @date 2024-06-24
 */
@Service
public class ProWorkOrderServiceImpl implements IProWorkOrderService {
    @Autowired
    private ProWorkOrderMapper proWorkOrderMapper;
    @Autowired
    @Lazy
    private IProWorkTaskService proWorkTaskService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询生产工单
     *
     * @param workOrderCode 生产工单主键
     * @return 生产工单
     */
    @Override
    public ProWorkOrder selectProWorkOrderByWorkOrderCode(String workOrderCode) {
        return proWorkOrderMapper.selectProWorkOrderByWorkOrderCode(workOrderCode);
    }

    /**
     * 查询生产工单列表
     *
     * @param proWorkOrder 生产工单
     * @return 生产工单
     */
    @Override
    public List<ProWorkOrder> selectProWorkOrderList(ProWorkOrder proWorkOrder) {
        return proWorkOrderMapper.selectProWorkOrderList(proWorkOrder);
    }

    /**
     * 新增生产工单
     *
     * @param proWorkOrder 生产工单
     * @return 结果
     */
    @Override
    public int insertProWorkOrder(ProWorkOrder proWorkOrder) {
        proWorkOrder.setCreateTime(DateUtils.getNowDate());
        proWorkOrder.setCreateBy(SecurityUtils.getUsername());
        return proWorkOrderMapper.insertProWorkOrder(proWorkOrder);
    }

    /**
     * 修改生产工单 排产
     *
     * @param proWorkOrder 生产工单
     * @return 结果
     */
    @Override
    public int updateProWorkOrder(ProWorkOrder proWorkOrder) {
        proWorkOrder.setUpdateTime(DateUtils.getNowDate());
        proWorkOrder.setUpdateBy(SecurityUtils.getUsername());
        return proWorkOrderMapper.updateProWorkOrder(proWorkOrder);
    }

    @Override
    public int arrangeWork(ProWorkOrder proWorkOrder, List<ProWorkTask> proWorkTasks) {
        // 生成 工单任务
        proWorkTasks.forEach(e -> {
            proWorkTaskService.insertProWorkTask(e);
        });
        proWorkOrder.setWorkOrderStatus("1");
        rabbitTemplate.convertAndSend("proStatusConsumer.exchange", "doingSchedule", proWorkOrder.getScheduleCode());
        return updateProWorkOrder(proWorkOrder);
    }

    /**
     * 批量删除生产工单
     *
     * @param workOrderCodes 需要删除的生产工单主键
     * @return 结果
     */
    @Override
    public int deleteProWorkOrderByWorkOrderCodes(List<String> workOrderCodes) {
        return proWorkOrderMapper.deleteProWorkOrderByWorkOrderCodes(workOrderCodes);
    }

    /**
     * 删除生产工单信息
     *
     * @param workOrderCode 生产工单主键
     * @return 结果
     */
    @Override
    public int deleteProWorkOrderByWorkOrderCode(String workOrderCode) {
        return proWorkOrderMapper.deleteProWorkOrderByWorkOrderCode(workOrderCode);
    }

    @Override
    public int deleteProWorkOrderByScheduleCode(String scheduleCode) {
        return proWorkOrderMapper.deleteProWorkOrderByScheduleCode(scheduleCode);
    }
}
